package com.zyx.javademo.clazz;

/**
 * @author Yaxi.Zhang
 * @since 2021/7/7 17:21
 * desc: Java继承中的动态绑定
 */
public class DynamicBind {
    public static void main(String[] args) {
        Parent p1 = new Son1();
        // 属性静态绑定
        // 输出结果: p1 name => parent
        System.out.println("p1 name => " + p1.name);
        // 方法动态绑定
        // 输出结果: son1 => son1
        p1.printInfo();

        Parent p2 = new Son2();
        System.out.println("p2 name => " + p2.name);
        // 方法动态绑定, 由于Son2中没有重写name属性, 因此会从父类中继承
        // 输出结果: son2 => parent
        p2.printInfo();
    }
}

class Parent {

    public String name = "parent";

    public void printInfo() {
        System.out.println("parent => " + name);
    }

}


class Son1 extends Parent {

    public String name = "son1";

    @Override
    public void printInfo() {
        System.out.println("son1 => " + name);
    }

}

class Son2 extends Parent {

    @Override
    public void printInfo() {
        System.out.println("son2 => " + name);
    }

}


